package com.neuedu.mybatis.dao;

import com.neuedu.mybatis.entity.Dept;
import com.neuedu.mybatis.entity.UmsUser;
import com.neuedu.mybatis.util.SessionFactoryUtils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class UmsUserDao {

    //线程安全的 重计量  全局唯一
    private SqlSessionFactory sqlSessionFactory = SessionFactoryUtils.getInstance();
    public UmsUserDao() throws IOException {
    }
//    public UmsUserDao() throws IOException {
//        String resource = "SqlMapConfig.xml";
//        InputStream inputStream = Resources.getResourceAsStream(resource);
//        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//    }

   


    /**
     * SqlSession 用户接口
     * @throws IOException
     */

    /**
     * 查询集合
     * @return
     * @throws IOException
     */
    public  List  selectList() throws IOException {
        //  SqlSession session   线程不安全的，每个线程自己独享
        SqlSession session =sqlSessionFactory.openSession();
        List<UmsUser> result = new ArrayList<>();
        try{
            //执行
            result = session.selectList("user.selectList");
        }catch (Exception e){
            e.printStackTrace();
        }

        session.close();

        return result;

    }


    /**
     * 根据主键查询单条结果
     * @param id
     * @return
     * @throws IOException
     */
    public  UmsUser selectOne(long id ) throws IOException {

        //  SqlSession session   线程不安全的，每个线程自己独享
        SqlSession session =sqlSessionFactory.openSession();
        UmsUser user = null;

        try{
            //执行
            user  = session.selectOne("user.selectById",id);
        }catch (Exception e){
            e.printStackTrace();
        }
        session.close();
        return user;
    }
    
    public  int insert(UmsUser user) throws IOException {
        //  SqlSession session   线程不安全的，每个线程自己独享
        SqlSession session =sqlSessionFactory.openSession();
        //执行
        int count = 0;
        try {
            count = session.insert("user.insert", user);
            session.commit();
        }catch (Exception e){
            session.rollback();
            e.printStackTrace();
        }
        session.close();

        return  count;
    }

    public  int update(UmsUser user) throws IOException {
        //  SqlSession session   线程不安全的，每个线程自己独享
        SqlSession session =sqlSessionFactory.openSession();
        //执行
        int count = 0;
        try {
            count = session.update("user.updateById", user);
            session.commit();
        }catch (Exception e){
            session.rollback();
            e.printStackTrace();
        }
        session.close();

        return  count;
    }

    public  int deleteById(long id) throws IOException {
        //  SqlSession session   线程不安全的，每个线程自己独享
        SqlSession session =sqlSessionFactory.openSession();
        //执行
        int count = 0;
        try {
            count = session.delete("user.deleteById", id);
            session.commit();
        }catch (Exception e){
            session.rollback();
            e.printStackTrace();
        }
        session.close();

        return  count;
    }

}
